package com.gangwangchina.android.loveynotify;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public abstract class cache {
//	public static int _DATA_CACHE_VERSION_ = 1;

	private static final String DB_NAME = "LoveyNotify.db";

	protected String tableName;

	private final Context mContext;

	protected CursorFactory mFactory;

	protected Map<String, String> tableContent;

	private SQLiteDatabase mDataBase;

	protected DatabaseHelper dbHelper;
	
	protected String mColumnIdInMedia;

	private boolean mIsInit;
	
	protected int _DATA_CACHE_VERSION_ = 0;

	public cache(Context ctx) {
		this.mContext = ctx;
		this.tableContent = new HashMap<String, String>();
		mIsInit = false;
		this.mFactory = null;
		mColumnIdInMedia = null;
	}

	protected abstract void _init();
	public void init() {
		try {
			if (mIsInit) {
				return;
			}

			_init();
			
			dbHelper = new DatabaseHelper(mContext, DB_NAME, mFactory,
					_DATA_CACHE_VERSION_);

			this.setTableAttr();

			dbHelper.setTableAttr(tableName, tableContent);
			mDataBase = dbHelper.getWritableDatabase();	
			
			mIsInit = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
	
	}

	protected abstract void setTableAttr();

	public boolean insert(ContentValues values) {

		if (mDataBase == null) {
			return false;
		}

		mDataBase.insert(this.tableName, null, values);

		return true;
	}

	public boolean delete(String whereClause) {

		if (mDataBase == null) {
			return false;
		}

		mDataBase.delete(this.tableName, whereClause, null);
		return true;
	}

	public boolean deleteAll() {
		if (mDataBase == null) {
			return false;
		}
		List<Integer> _ids = this.getIds();
		for(int id : _ids) {
			mDataBase.delete(this.tableName, DatabaseHelper.COLUMN_ID + "=" + id, null);
		}
		
		return true;
	}
	
	
	public boolean update(ContentValues values, String whereClause) {
		if (mDataBase == null) {
			return false;
		}

		mDataBase.update(this.tableName, values, whereClause, null);
		return true;
	}

	private Cursor query(String columnName, int id) {
		if (mDataBase == null) {
			return null;
		}

		if( mColumnIdInMedia == null ||
				mColumnIdInMedia.equalsIgnoreCase("")) {
			mColumnIdInMedia = DatabaseHelper.COLUMN_ID;
		}
		
		Cursor cursor = mDataBase.query(this.tableName, new String[] {columnName, mColumnIdInMedia}, null, null, null,
				null, "_id asc");
		
		int _idIndex = cursor.getColumnIndex(mColumnIdInMedia);
		
		if(_idIndex != -1) {
			for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
				if( id == cursor.getInt(_idIndex)) {
					return cursor;
				}
			}	
		}			

		cursor.close();
		return null;
		
	}
	
	private Cursor query(String columnName) {
		if (mDataBase == null) {
			return null;
		}

		if( mColumnIdInMedia == null ||
				mColumnIdInMedia.equalsIgnoreCase("")) {
			mColumnIdInMedia = DatabaseHelper.COLUMN_ID;
		}
		
		Cursor cursor = mDataBase.query(this.tableName, new String[] {columnName, mColumnIdInMedia}, null, null, null,
				null, "_id asc");
	
//		cursor.close();
		return cursor;
		
	}
	
	public String queryString(String columnName, int id)
	{
		Cursor cursor = query( columnName, id);	
		
		String value = null;
		
		if(cursor != null) {
			value = cursor.getString(0);
			cursor.close();
		}
		
		return value;
	}
	
	public int queryInteger(String columnName, int id)
	{
		Cursor cursor = query( columnName, id);	
		
		int value = -1;
		
		if(cursor != null) {
			value = cursor.getInt(0);
			cursor.close();
		}
		
		return value;
	}
	
	public long queryLong(String columnName, int id)
	{
		Cursor cursor = query( columnName, id);	
		
		long value = -1;
		
		if(cursor != null) {
			value = cursor.getLong(0);
			cursor.close();
		}
		
		return value;
	}
	
	public double queryDouble(String columnName, int id) {
		Cursor cursor = query( columnName, id);	
		
		double value = -1;
		
		if(cursor != null) {
			value = cursor.getDouble(0);			
			cursor.close();
		}
		
		return value;
	}
	
	public byte[] queryByte(String columnName, int id) {
		Cursor cursor = query( columnName, id);	
		
		byte[] value = new byte[1024 * 100];
		
		if(cursor != null) {
			value = cursor.getBlob(0);
			cursor.close();
		}
		
		return value;
	}

	public void Release() {

		if( this.mDataBase != null ) {
			this.mDataBase.close();
			this.mDataBase = null;
		}
		
		this.mIsInit = false;
	}
	
	public int getCount() {
		int count = 0;
		Cursor cursor = query( DatabaseHelper.COLUMN_ID );
		
		if (cursor != null) {
			count = cursor.getCount();
			cursor.close();
		}
		return count;
	}
	
	public List<Integer> getIds() {
		List<Integer> ids = new LinkedList<Integer>();
		Cursor cursor = query( DatabaseHelper.COLUMN_ID );		
		
		if (cursor != null) {
			int _idIndex = cursor.getColumnIndex(DatabaseHelper.COLUMN_ID);
			for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
				ids.add(Integer.valueOf(cursor.getInt(_idIndex)));			
			}	
			cursor.close();
		}
		return ids;
	}
}
